﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using System.Data;
using MaxLeaf.DB;
using System.Data.SqlClient;

namespace MaxLeaf.DAL
{
    public class t_Base_EmpDALBuilder :   BaseDALBuilder
    {
        

        public t_Base_EmpDALBuilder()
            : base()
        {
            K2WDAL =  new K2W.t_Base_EmpDAL();
 

        }
        protected override bool ValidCheckOrgData(ServerInfo orgServer, DataRowView drvLog)
        {
             
            if ((LogStatus)CvtFunc.ToInt(drvLog[DEL.LogDEL.LogStatus]) == LogStatus.Delete)
            {
                return true;
            }
            using (DBAccess dbOrgCon = new DBAccess(orgServer))
            {
                string strFItemId = CvtFunc.ToString(drvLog[DEL.LogDEL.KeyValue]);
                DataRow drOrgData = dbOrgCon.GetTableDataFirstRow("t_Base_Emp", new Dictionary<string, object>() { { "FItemId", strFItemId } }, "FNumber,FDepartmentID");
                if (drOrgData == null)
                {
                    LogManager.Debug(string.Format("t_Base_Emp {0} not exist", strFItemId));
                    return false;
                }

                //只分发4开头的
                if (!CvtFunc.ToString(drOrgData["FNumber"]).StartsWith("4.", StringComparison.InvariantCultureIgnoreCase)
                    
                    )
                {
                    //if (CvtFunc.ToInt(drOrgData["FDepartmentID"]) > 0 && dbOrgCon.GetValue("select fnumber from t_department where fitemid=@fitemid", new SqlParameter[] { new SqlParameter("@fitemid", drOrgData["FDepartmentID"]) }).StartsWith("4."))
                    //{
                    //    //部门4开头也分发
                    //}
                    //else{
                    LogManager.Debug(string.Format("t_Base_Emp {0} FNumber {1} 不分发", strFItemId, drOrgData["FNumber"]));
                    return false;
                    //}
                }

                //并且部门也要4开头的
                if (CvtFunc.ToInt(drOrgData["FDepartmentID"]) > 0 )
                {
                    string strDepFNumber= dbOrgCon.GetValue("select fnumber from t_department where fitemid=@fitemid", new SqlParameter[] { new SqlParameter("@fitemid", drOrgData["FDepartmentID"]) });
                    if (!strDepFNumber.StartsWith("4.", StringComparison.InvariantCultureIgnoreCase))
                    {
                        LogManager.Debug(string.Format("t_Base_Emp {0}  部门 FNumber {1} 不分发", strFItemId, strDepFNumber));
                        return false;
                    }
                }

            }
            return base.ValidCheckOrgData(orgServer, drvLog);
        }
        
        
    }
}